package com.web.exiaodao.remote.service.impl;

import com.alibaba.fastjson.JSON;
import com.web.exiaodao.business.constant.JConstant;
import com.web.exiaodao.business.entity.Address;
import com.web.exiaodao.business.repository.AddressRepository;
import com.web.exiaodao.remote.service.ApiAddressService;
import com.web.exiaodao.remote.vo.AddressVo;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.util.XaUtil;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: zhangl
 * @times: 2015-5-15下午06:46:35
 * 类的说明：前端APIAddress接口实现类
 **/
@Service("ApiAddressService")
@Transactional(readOnly = false)
public class ApiAddressServiceImpl extends BaseService<Address> implements ApiAddressService{

	@Autowired
	AddressRepository addressRepository;

	@Override
	public XaResult<AddressVo> findOne(Long tId) throws BusinessException {
		Address obj = addressRepository.findByIdAndStatusNot(tId,XaConstant.Status.delete);
		XaResult<AddressVo> xr = new XaResult<AddressVo>();
		if (XaUtil.isNotEmpty(obj)) {
			xr.setObject(JSON.parseObject(JSON.toJSONString(obj),AddressVo.class));
			xr.getObject().setId(obj.getId());
		} else {
			throw new BusinessException(XaConstant.Message.object_not_find);
		}
		return xr;
	}

	@Override
	public XaResult<List<AddressVo>> findListNEStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable)
			throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示非删除的所有数据
			status = XaConstant.Status.delete;
		}
		filters.put("status", new SearchFilter("status", Operator.NE, status));
		Page<Address> page = addressRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), Address.class), pageable);
		XaResult<List<AddressVo>> xr = new XaResult<List<AddressVo>>();
		xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), AddressVo.class));
		for(int i=0;i<page.getContent().size();i++){
			xr.getObject().get(i).setId(page.getContent().get(i).getId());
		}
		return xr;
	}

	@Override
	public XaResult<List<AddressVo>> findListEQStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable)
			throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示正常数据
			status = XaConstant.Status.valid;
		}
		filters.put("status", new SearchFilter("status", Operator.EQ, status));
		Page<Address> page = addressRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), Address.class), pageable);
		XaResult<List<AddressVo>> xr = new XaResult<List<AddressVo>>();
		xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), AddressVo.class));
		for(int i=0;i<page.getContent().size();i++){
			xr.getObject().get(i).setId(page.getContent().get(i).getId());
		}
		//默认收货地址排在第一
		AddressVo defaultAdress = null;
		for (int i = 0 ; xr.getObject() != null && i < xr.getObject().size(); ++i) {
			AddressVo vo = xr.getObject().get(i);
			if (vo.getIsDefault().intValue() == JConstant.BooleanStatus.TRUE) {
				defaultAdress = vo;
				break;
			}
		}
		if (XaUtil.isNotEmpty(xr.getObject())) {
			xr.getObject().remove(defaultAdress);
		}
		if (XaUtil.isNotEmpty(defaultAdress)) {
			xr.getObject().add(0, defaultAdress);
		}
		return xr;
	}

	@Override
	public XaResult<AddressVo> multiOperate(String modelIds,
											Integer status) throws BusinessException {
		XaResult<AddressVo> xr = new XaResult<AddressVo>();
		if(status == null){
			status = XaConstant.Status.delete;
		}
		if(modelIds != null){
			String[] ids = modelIds.split(",");
			for(String id : ids){
				Address obj = addressRepository.findByIdAndStatusNot(Long.parseLong(id),status);
				if (XaUtil.isNotEmpty(obj)) {
					obj.setStatus(status);
					obj = addressRepository.save(obj);
					xr.setObject(JSON.parseObject(JSON.toJSONString(obj), AddressVo.class));
					xr.getObject().setId(obj.getId());
				} else {
					throw new BusinessException(XaConstant.Message.object_not_find);
				}
			}
		}
		return xr;
	}

	@Override
	public XaResult<AddressVo> createModel(Address model)
			throws BusinessException {
        XaResult<AddressVo> xr = new XaResult<AddressVo>();
        Address obj = null;
        if(model.getIsDefault()==JConstant.BooleanStatus.TRUE){
            List<Address> address=addressRepository.findByUserIdAndStatusNot(model.getUserId(), XaConstant.Status.delete);
            if(address.size()>0){
                String sql="update tb_xa_address set is_default=" + JConstant.BooleanStatus.FALSE + " where user_id=? and status<>" + XaConstant.Status.delete;
                List<Object> params=new ArrayList<Object>();
                params.add(model.getUserId());
                Integer count=this.update(sql, params);
                if(count>0){
                    obj = addressRepository.save(model);
                }else{
                    xr.error("新增失败！");
                    return xr;
                }
            }else{
                obj = addressRepository.save(model);
            }
        }else{
            obj = addressRepository.save(model);
        }
        xr.setObject(JSON.parseObject(JSON.toJSONString(obj), AddressVo.class));
        xr.getObject().setId(obj.getId());
        return xr;
	}

	@Override
	public XaResult<AddressVo> updateModel(Address model)
			throws BusinessException {
		Address obj = addressRepository.findOne(model.getId());
		XaResult<AddressVo> xr = new XaResult<AddressVo>();
		if (XaUtil.isNotEmpty(obj)) {
			obj.setConsigneeName(XaUtil.isNotEmpty(model.getConsigneeName()) ? model.getConsigneeName() : obj.getConsigneeName());
			obj.setMobile(XaUtil.isNotEmpty(model.getMobile()) ? model.getMobile() : obj.getMobile());
			obj.setProvince(XaUtil.isNotEmpty(model.getProvince()) ? model.getProvince() : obj.getProvince());
			obj.setCity(XaUtil.isNotEmpty(model.getCity()) ? model.getCity() : obj.getCity());
			obj.setArea(XaUtil.isNotEmpty(model.getArea()) ? model.getArea() : obj.getArea());
			obj.setDetailAddress(XaUtil.isNotEmpty(model.getDetailAddress()) ? model.getDetailAddress() : obj.getDetailAddress());
			obj.setUserId(XaUtil.isNotEmpty(model.getUserId()) ? model.getUserId() : obj.getUserId());
			obj.setIsDefault(XaUtil.isNotEmpty(model.getIsDefault()) ? model.getIsDefault() : obj.getIsDefault());
			obj.setPostcode(XaUtil.isNotEmpty(model.getPostcode()) ? model.getPostcode() : obj.getPostcode());

			/*如果是否默认不为空，如果该地址为默认地址不可将此地址改为非默认地址；
			 *如果该地址为非默认地址，将此地址改为默认地址，要将该用户之前的默认地址改为非默认地址*/
			if(XaUtil.isNotEmpty(model.getIsDefault())){
				if(model.getIsDefault()==JConstant.BooleanStatus.TRUE){
					List<Address> address=addressRepository.findByUserIdAndStatusNot(obj.getUserId(), XaConstant.Status.delete);
					if(address.size()>1){
						String sql="update tb_xa_address set is_default=" + JConstant.BooleanStatus.FALSE + " where user_id=? and " +
								"id<>"+ obj.getId() +" and status<>" + XaConstant.Status.delete;
						List<Object> params=new ArrayList<Object>();
						params.add(obj.getUserId());
						Integer count=this.update(sql, params);
						if(count>0){
							obj.setIsDefault(model.getIsDefault());
							obj = addressRepository.save(obj);
						}else{
							xr.error("地址修改失败！");
							return xr;
						}
					}else{
						obj.setIsDefault(model.getIsDefault());
						obj = addressRepository.save(obj);
					}
				}
				if(model.getIsDefault()== JConstant.BooleanStatus.FALSE){
					obj.setIsDefault(model.getIsDefault());
					obj = addressRepository.save(obj);
				}
				/*if(model.getIsDefault()==JConstant.BooleanStatus.FALSE){
					if(obj.getIsDefault()==JConstant.BooleanStatus.TRUE){
						xr.error("不能将此地址改为非默认地址！必须有一个默认地址");
						return xr;
					}
					obj.setIsDefault(model.getIsDefault());
					obj = addressRepository.save(obj);
				}*/
			}else{
				obj = addressRepository.save(obj);
			}

			xr.setObject(JSON.parseObject(JSON.toJSONString(obj), AddressVo.class));
			xr.getObject().setId(obj.getId());
		} else {
			throw new BusinessException(XaConstant.Message.object_not_find);
		}
		return xr;
	}

}
